package DynamicProgrammingPackage;

public class maxProfitV {

    // 1. dp[i][0]表示第i天持股
    // dp[i][1]表示第i天保持卖出股
    // dp[i][2]表示第i天卖出股票
    // dp[i][3]表示第i天冷冻期
    // 2. 递推公式:
    // dp[i][0] = Math.max(dp[i - 1][0],dp[i - 1][1] - prices[i],dp[i - 1][3] - prices[i])
    // dp[i][1] = Math.max(dp[i - 1][1],dp[i - 1][3])
    // dp[i][2] = dp[i][0] + prices[i]
    // dp[i][3] = dp[i - 1][2]

    public int maxProfit(int[] prices) {
        int[][] dp = new int[prices.length][4];
        dp[0][0] = -prices[0];
        dp[0][1] = 0;
        dp[0][2] = 0;
        dp[0][3] = 0;
        for (int i = 1; i < prices.length; i++) {
            dp[i][0] = Math.max((Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i])), dp[i - 1][3] - prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
            dp[i][2] = dp[i][0] + prices[i];
            dp[i][3] = dp[i - 1][2];
        }
        return Math.max(Math.max(dp[prices.length - 1][0], dp[prices.length - 1][1]), Math.max(dp[prices.length - 1][2], dp[prices.length - 1][3]));
    }
}
